package my.design.interpreter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 * @author zp
 * @version 1.0
 * @description: 测试客户端
 * @date 2022/10/28 19:03
 */
public class Client {
    public static void main(String[] args) throws Exception {
        //获得表达式
        String expStr = getExpStr();
        //获得值映射
        HashMap<String, Double> var = getValue(expStr);
        //封装解析器
        Calculator calculator = new Calculator(expStr);
        System.out.println("运算结果：" + expStr + "=" + calculator.run(var));
    }

    //获得表达式
    public static String getExpStr() throws IOException {
        System.out.print("请输入表达式：");
        return (new BufferedReader(new InputStreamReader(System.in))).readLine();
    }

    //获得值映射
    public static HashMap<String, Double> getValue(String expStr) throws Exception {
        HashMap<String, Double> map = new HashMap<>(16);

        for (char ch : expStr.toCharArray()) {
            if (ch != '+' && ch != '-' && ch != '*' && ch != '/' && ch != '(' && ch != ')') {
                if (!map.containsKey(String.valueOf(ch))) {
                    System.out.print("请输入" + ch + "的值：");
                    String in = (new BufferedReader(new InputStreamReader(System.in))).readLine();
                    map.put(String.valueOf(ch), Double.valueOf(in));
                }
            }
        }
        return map;
    }
}
